package x.trident.core.pojo.response;

import lombok.Data;

/**
 * http响应结果封装
 *
 * @author 林选伟
 * @date 2020/10/17 17:33
 */
@Data
public class ResponseData<T> {

    /**
     * 请求是否成功
     */
    private Boolean success;

    /**
     * 响应状态码
     */
    private String code;

    /**
     * 响应信息
     */
    private String message;


    /**
     * 异常的提示信息
     */
    private String exceptionTip;

    /**
     * 响应对象
     */
    private T data;

    public ResponseData() {
    }

    public ResponseData(Boolean success, String code, String message, T data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ResponseData(T data) {
        this.success=true;
        this.code = CodeMsg.OK.getCode();
        this.message = CodeMsg.OK.getDefaultMsg();
        this.data = data;
    }

    public ResponseData(CodeMsg msg) {
        if (msg != null) {
            this.success=false;
            this.code = msg.getCode();
            this.message = msg.getDefaultMsg();
        }
    }

    public ResponseData(CodeMsg msg,String msgStr) {
        if (msg != null) {
            this.success=false;
            this.code = msg.getCode();
            this.message = msgStr;
        }
    }

    public ResponseData(CodeMsg msg,Throwable e) {
        if (msg != null) {
            this.success=false;
            this.code = msg.getCode();
            this.message = msg.getDefaultMsg();
            this.exceptionTip = e.getMessage();
        }
    }

    public static <T> ResponseData<T> success(T data) {
        return new ResponseData(data);
    }

    public static <T> ResponseData<T> success() {
        return success((T)null);
    }

    public static <T> ResponseData<T> error(String code, String msg) {
        return fail(new CodeMsg(code, msg));
    }

    public static <T> ResponseData<T> error(String msg) {
        return fail(new CodeMsg("400", msg));
    }

    public static <T> ResponseData<T> error(Boolean success, String code, String msg, T errDetails) {
        return new ResponseData(success,code, msg, errDetails);
    }

    public static <T> ResponseData<T> fail(CodeMsg msg) {
        return new ResponseData(msg);
    }

    public static <T> ResponseData<T> fail(CodeMsg msg,String msgStr) {
        return new ResponseData(msg,msgStr);
    }

    public static <T> ResponseData<T> fail(CodeMsg msg,Throwable e) {
        return new ResponseData(msg,e);
    }

    public Object getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

//    @Override
//    public String toString() {
//        return "ResponseVo [success="+this.success+",code=" + this.code + ", message=" + this.message + ", data=" + this.data + "]";
//    }


}
